const app=getApp()
const myRequest = (url, data,method='GET') => {


  data['token'] = 'Bearer ' + app.globalData.token;
  return new Promise((res, rej) => {
    wx.request({
      url: app.globalData.apiurl + url,
      method: method,
      data: data,
      header: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + app.globalData.token},
      success(obj) {
        if (obj.data.message === 'Unauthenticated.') {
          wx.redirectTo({
            url: '/pages/login/index',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })}
        if (obj.statusCode >= 200 && obj.statusCode < 300 || obj.statusCode === 304) {
          res(obj.data);
        } 
         else {
          rej({
            msg: `网络错误:${obj.statusCode}`,
            detail: obj
          });
        }
      },
      fail(err) {
        rej(err);
      }
    })
  })
}
module.exports = {
  myRequest
}